package com.zxl.always.on.display.ui.widget.clock.digital;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.zxl.always.on.display.R;
import com.zxl.always.on.display.model.PaletteSet;
import com.zxl.always.on.display.tools.FontUtils;
import com.zxl.always.on.display.tools.GraphicUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


/**
 * Created by Administrator on 2018/2/28.
 */

public class ImageDigitalClockWidget extends BaseTimeViewHolder {

    private TextView mTimerView, mAmAndPmView;

    private TextView mCalendarView;

    public ImageDigitalClockWidget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTimerView = findViewById(R.id.panel_clock_time);
        mAmAndPmView = findViewById(R.id.clock_ampm_right);
        mCalendarView = findViewById(R.id.panel_clock_calendar);
        if (isInEditMode()) {
            return;
        }
        mTimerView.setTypeface(FontUtils.getTypeface());
    }

    @Override
    public void refreshTime() {
        if (DateFormat.is24HourFormat(getContext())) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            mTimerView.setText(dateFormat.format(System.currentTimeMillis()));
            mAmAndPmView.setVisibility(View.GONE);
        } else {
            SimpleDateFormat dateFormatA = new SimpleDateFormat("a", Locale.getDefault());
            mAmAndPmView.setVisibility(View.VISIBLE);
            mAmAndPmView.setText(dateFormatA.format(System.currentTimeMillis()));
            SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm", Locale.getDefault());
            mTimerView.setText(dateFormat.format(System.currentTimeMillis()));
        }
        Date date = Calendar.getInstance().getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat(getResources().getString(R.string.calendar_date_full_format), Locale.getDefault());
        mCalendarView.setText(dateFormat.format(date));
    }

    @Override
    public void setPaletteColor(int position) {
        GraphicUtils.setLinearGradientTextColor(mTimerView, PaletteSet.getPaletteItems().get(position));
        GraphicUtils.setLinearGradientTextColor(mAmAndPmView, PaletteSet.getPaletteItems().get(position));
    }
}
